package org.chen;

import org.chen.pool.JedisConnect;

import java.util.HashMap;

public class ZsetJedisTest {
  public static void main(String[] args) {
    JedisConnect connect = new JedisConnect();
    connect.execute(jedis -> {
      jedis.flushDB();

      //ZSet与set是不包含相同field的合集,ZSet与set的区别是ZSet是依靠score排序的有序集合，而set是无序集合

      //存
      jedis.zadd("a1",10,"a");

      HashMap<String, Double> map = new HashMap<>();
      map.put("b",20.0);
      map.put("c",30.0);
      jedis.zadd("a1",map);

      //获取key中member的score
      System.out.println(jedis.zscore("a1", "a"));//10.0

      //返回key中指定范围的member(按score从小到大排序)
      System.out.println(jedis.zrange("a1", 0, -1));//[a, b, c]

      //返回key中指定范围的member(按score从大到小排序)
      System.out.println(jedis.zrevrange("a1", 0, -1));//[c, b, a]

      //返回key中member的个数
      System.out.println(jedis.zcard("a1"));//3

      HashMap<String, Double> map2 = new HashMap<>();
      map2.put("d",40.0);
      map2.put("e",50.0);
      map2.put("f",60.0);
      map2.put("g",70.0);
      jedis.zadd("a1",map2);

      //返回key中score在mix和max之间的member的个数
      // 包含min和max，闭区间
      System.out.println(jedis.zcount("a1", 30, 70));//5
      //不包含min和max，'('：开区间
      System.out.println(jedis.zcount("a1", "(30", "(70"));//3

      //返回key中score在mix和max之间的member
      System.out.println(jedis.zrangeByScore("a1", 20, 60));//[b, c, d, e, f]

      //返回member在key中按score从小到大排序中的所在排位
      System.out.println(jedis.zrank("a1", "a"));//0
      System.out.println(jedis.zrank("a1", "g"));//6

      //返回member在key中按score从大到小排序中的所在排位
      System.out.println(jedis.zrevrank("a1", "a"));//6
      System.out.println(jedis.zrevrank("a1", "g"));//0

      //key中的指定score按整数步长自增
      System.out.println(jedis.zincrby("a1", 5, "g"));//75.0


      HashMap<String, Double> map3 = new HashMap<>();
      map3.put("b",20.0);
      map3.put("c",30.0);
      jedis.zadd("a2",map3);

      //将多个集合的交集存到新集合中，新集合中的score是两个交集元素的score之和
      jedis.zinterstore("a3","a1","a2");
      System.out.println(jedis.zrange("a3", 0, -1));//[b, c]
      System.out.println(jedis.zscore("a3", "b"));//40.0（=a1.b:20+a2.b:20）
      System.out.println(jedis.zscore("a3", "c"));//60.0(=a1.c:30+a2.c:30）

      //移除一个指定member
      jedis.zrem("a1","a");
      System.out.println(jedis.zrange("a1", 0, -1));//[b, c, d, e, f, g]

      //获取集合中指定member与member之间的member个数
      //所有member个数
      System.out.println(jedis.zlexcount("a1", "-", "+"));//6
      //member与member之间的member个数,需要在member前加'['
      System.out.println(jedis.zlexcount("a1", "[b", "[e"));//4

      //获取集合中指定member与member之间的member
      //所有member
      System.out.println(jedis.zrangeByLex("a1", "-", "+"));//[b, c, d, e, f, g]
      //member与member之间的member,包含member与member，'['代表闭区间
      System.out.println(jedis.zrangeByLex("a1", "[b", "[e"));//[b, c, d, e]
      //member与member之间的member,不包含member与member，'('代表开区间
      System.out.println(jedis.zrangeByLex("a1", "(b", "(e"));//[c, d]

    });
  }
}
